package com.bridge.common.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

import com.bridge.common.R;
import com.bridge.common.base.BaseAppLication;
import com.bridge.common.http.HttpUtils;

/**
 *
 */
public class VDialog {
    private static VDialog instance = null;
    private MyPopupWindow pw;
    private LoadingDialog mLoadingDlg = null;
    private int deviceWidth = 0;
    private int deviceHeight = 0;

    public VDialog() {
        getDeviceScreenSize();
    }

    private static class SingleHolder {
        private static VDialog instance = new VDialog();
    }


    public static synchronized VDialog getInstance() {
        return SingleHolder.instance;
    }

    /**
     * 函数名 ：getDeviceScreenSize
     * <p>
     * 功能描述 ：获取设备尺寸
     */
    private void getDeviceScreenSize() {
        Display dis = ((WindowManager) BaseAppLication.getContext().getSystemService(Context.WINDOW_SERVICE))
                .getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        dis.getMetrics(metrics);
        deviceWidth = metrics.widthPixels;
        deviceHeight = metrics.heightPixels;

    }

    /**
     * 简单版本的展示加载中的view,触摸空白可关闭,网络请求成功后自动关闭
     * @param activity
     */
    public static void showLoading(Activity activity) {
        VDialog.getInstance().showLoadingDialog(activity.getResources().getString(R.string.loading),activity,true,true);
    }

    /**
     * 显示loding框
     */
    public void showLoadingDialog(String content, Activity currActy, boolean cancelable, boolean autoClose) {
        hideLoadingDialog();
        if (currActy != null && !currActy.isFinishing()) {
            mLoadingDlg = new LoadingDialog(currActy, R.style.showLoadingDialogStyle);
            mLoadingDlg.setDialogLayout(R.layout.loading_tip);
            // 如果上面传递下来是TRUE则不消失框
            mLoadingDlg.setDialogCanceable(cancelable);
            mLoadingDlg.setTipContent(content);
            mLoadingDlg.setCanceledOnTouchOutside(false);
            if (mLoadingDlg != null && !mLoadingDlg.isShowing()) {
                try {
                    mLoadingDlg.show();
                }catch (Exception e){
                }
            }
            mLoadingDlg.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialogInterface) {
                    mLoadingDlg = null;
                    instance = null;
                    //中断请求
                    HttpUtils.getInstance().cancle();
                }
            });
        }
    }

    public static void hideLoading() {
        VDialog.getInstance().hideLoadingDialog();
    }

    /**
     * 隐藏loding框 返回true，表示有dlg显示， false 没有dlg在显示
     */
    public boolean hideLoadingDialog() {
        if (mLoadingDlg != null) {
            mLoadingDlg.dismiss();
            mLoadingDlg = null;
        }
        return true;

    }

    /**
     * @method: closePw @Description: 关闭和释放资源 @throws
     */
    public void closePw() {
        if (pw != null) {
            Activity attach = pw.getAttachActivity();
            if (attach != null && !attach.isFinishing()) {
                pw.closePopupWindow();
                pw = null;
            }
        }
    }

    public abstract static interface OnDialogDismissListener {
        public void onDismiss();
    }

    public interface GetDatePickerResultListener {
        void getDatePickerResult(String date);
    }
}
